﻿using System.IO;
using System.Windows.Media;
using SvgViewer.Core;
using SvgViewer.Utilities;

namespace SvgViewer.Entities;

/// <summary>
/// Svg 信息
/// </summary>
public class SvgInfo : NotifyPropertyBase
{
    private bool        isSelected;
    private bool        isValid;
    private ImageSource imageSource;

    /// <summary>
    /// 构造
    /// </summary>
    /// <param name="filePath"></param>
    public SvgInfo(string filePath)
    {
        this.FilePath = filePath;
        this.Name          = Path.GetFileNameWithoutExtension(filePath);
        this.IsValid       = true;
    }

    /// <summary>
    /// 文件路径
    /// </summary>
    public string FilePath {
        get;
    }

    /// <summary>
    /// 是否为有效的Svg文件
    /// </summary>
    public bool IsValid {
        get => isValid;
        set => SetValue(ref isValid, value, nameof(IsValid));
    }

    /// <summary>
    /// 文件名
    /// </summary>
    public string Name {
        get;
    }

    /// <summary>
    /// 获取或设置选中状态
    /// </summary>
    public bool IsSelected {
        get => isSelected;
        set => SetValue(ref isSelected, value, nameof(IsSelected));
    }

    /// <summary>
    /// 获取 Svg 文件内容
    /// </summary>
    public string Content
    {
        get
        {
            if (!IsValid)
            {
                return string.Empty;
            }

            try
            {
                var xaml = SvgHelper.SvgObjectToXaml(ImageSource);
                return xaml;
            }
            catch
            {
                return string.Empty;
            }
        }
    }

    /// <summary>
    /// 获取 <see cref="ImageSource"/>
    /// </summary>
    public ImageSource ImageSource {
        get {
            if (isValid && imageSource == null)
            {
                try
                {
                    imageSource = SvgHelper.SvgToImageSource(this.FilePath);
                }
                catch
                {
                    isValid = false;
                }
            }

            return imageSource;
        }
    }
}